ass Solution {
    int idx;
    unordered_map<int, int> idx_map;

public:
    TreeNode* helper(int inleft, int inright, vector<int>& inorder,
                     vector<int>& postorder) {
        if (inleft > inright) {
            return nullptr;
        }
        // idx为当前子树根节点
        int rval = postorder[idx];
        TreeNode* root = new TreeNode(rval);

        int index = idx_map[rval];

        idx--;
        root->right = helper(index + 1, inright, inorder, postorder);
        root->left = helper(inleft, index - 1, inorder, postorder);
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        // 后序遍历的每一个节点 都是根节点
        // 可以利用后续遍历在中序遍历里面找 离得最近的就是左右节点
        idx = (int)postorder.size() - 1;

        int iidx = 0;
        for (auto& val : inorder) {
            idx_map[val] = iidx++;
        }
        return helper(0, (int)inorder.size() - 1, inorder, postorder);
    }
};
